<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>更改密码</title>
        <link rel="stylesheet" href="css/conmmon.css">
        <link rel="stylesheet" href="css/login.css">
        <script src="js/jquery.min.js"></script>
    </head>
<body>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的注册框 -->
        <div class="login-dialog">
            <h3>更改密码</h3>
            <div class="row">
                <span>初始密码</span>
                <input type="text" id="oldPassword">
            </div>
            <div class="row">
                <span>新密码</span>
                <input type="password" id="newPassword">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="newPassword2">
            </div>
            <div class="row">
                <button id="submit" onclick="mysub()">提交</button>
            </div>
        </div>
    </div>
    <script>
        // 提交注册事件
        function mysub(){
            // 1.非空校验
            var oldPassword = jQuery("#oldPassword");
            var newPassword = jQuery("#newPassword");
            var newPassword2 = jQuery("#newPassword2");
            if(oldPassword.val()==""){
                alert("请先输入旧密码！");
                oldPassword.focus();// 将鼠标光标移动到此控件上
                return;
            }
            if(newPassword.val()==""){
                alert("请先输入密码！");
                newPassword.focus();
                return;
            }
            if(newPassword2.val()==""){
                alert("请先输入确认密码！");
                newPassword2.focus();
                return;
            }
            // 2.判断两次密码是否正确
            if(newPassword.val()!=newPassword2.val()){
                alert("两次密码输入的不一致请先检查！");
                return;
            }
            jQuery.ajax({
                url:"/user/password",
                type:"POST",
                data:{"oldPassword":oldPassword.val(),"newPassword":newPassword.val()},
                success:function(result){
                    // 响应结果
                    if(result!=null && result.code==200 && result.data==1){
                        // 执行成功
                        if(confirm("更改成功！是否重新登录？")){
                            location.href = "/login.html";
                        }
                    }else if(result != null && result.code == -2){
                        alert("密码错误")
                    }else{
                        alert("非法参数")
                    }
                }
            });
        }
    </script>
</body>
</html>